package order

import (
	"context"
	"go-zero-all-demos/apps/order/pb/order"

	"go-zero-all-demos/apps/api/internal/svc"
	"go-zero-all-demos/apps/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddOrderLogic {
	return &AddOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddOrderLogic) AddOrder(req *types.OrderInfo) (resp *types.OrderID, err error) {
	userId := l.ctx.Value("user_id").(int64)
	o, err := l.svcCtx.OrderRPC.AddOrder(l.ctx, &order.OrderInfo{UserId: userId, OrderId: req.OrderId})
	if err != nil {
		return nil, err
	}
	return &types.OrderID{Id: o.Id}, nil
}
